package com.jfeat.am.module.wegou.services.domain.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.jfeat.am.core.support.BeanKit;
import com.jfeat.am.module.wegou.services.domain.dao.QueryProductTypeDao;
import com.jfeat.am.module.wegou.services.domain.service.QueryProductTypeService;
import com.jfeat.am.module.wegou.services.persistence.dao.ProductTypeMapper;
import com.jfeat.am.module.wegou.services.persistence.model.ProductType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author admin
 * @since 2017-10-16
 */
@Service
public class QueryProductTypeServiceImpl implements QueryProductTypeService {

    @Resource
    QueryProductTypeDao queryProductTypeDao;
    @Resource
    ProductTypeMapper productTypeMapper;

    @Override
    public List<ProductType> findProductTypePage(Page<ProductType> page, ProductType productType) {
        return queryProductTypeDao.findProductTypePage(page, productType);
    }

    public List<Map<String, Object>> recurisivelyFindProductTypesPage(Page page, ProductType productType) {
        List<ProductType> rootProductTypes = queryProductTypeDao.findRootProductTypesPage(page, productType);
        return recursivelyGetChildren(rootProductTypes);
    }

    private List<Map<String, Object>> recursivelyGetChildren(List<ProductType> productTypes) {
        List<Map<String, Object>> result = Lists.newArrayList();
        for (ProductType productType : productTypes) {
            Map<String, Object> map = BeanKit.beanToMap(productType);
            List<ProductType> children = productTypeMapper.selectList(
                    new EntityWrapper<ProductType>().eq(ProductType.PID, productType.getId()).orderBy(ProductType.SORT_NUMBER, true)
            );
            if (children.size() > 0) {
                List<Map<String, Object>> recursivelyChildren = recursivelyGetChildren(children);
                map.put("children", recursivelyChildren);
            }
            result.add(map);
        }
        return result;
    }

    public List<Map<String, Object>> recurisivelyFindAllProductTypes() {
        List<ProductType> rootProductTypes = queryProductTypeDao.findAllRootProductTypes();
        return recursivelyGetChildren(rootProductTypes);
    }

    public List<Map<String, Object>> recurisivelyFindAllProductTypes(Long productRestaurantId) {
        List<ProductType> rootProductTypes = queryProductTypeDao.findAllRootProductTypes();
        List<ProductType> rootProductTypesOfARestaurant = Lists.newArrayList();
        for (ProductType productType : rootProductTypes) {
            if (productRestaurantId.equals(productType.getStoreId())) {
                rootProductTypesOfARestaurant.add(productType);
            }
        }
        return recursivelyGetChildren(rootProductTypesOfARestaurant);
    }


}
